package com.goods.shop.service.wxmessage;

import com.goods.shop.model.finals.WxFinals;
import com.goods.shop.model.wx.WxEventUrlType;
import com.goods.shop.service.AbstractService;
import com.goods.shop.service.constants.WeiXinUrlFinals;
import com.goods.shop.service.model.wxmessage.WxTextModel;
import com.goods.shop.util.WeiXinUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class WxBussinessServiceImpl extends AbstractService implements WxBussinessService {
    @Override
    public String generateSubscribeEvent(WxTextModel entity) {
        String content="";

        if(StringUtils.hasText(entity.getEventKey())){
            String eventyKey=entity.getEventKey().replaceAll(WxFinals.QRSCENE,"");
            WxEventUrlType type=WxEventUrlType.BURL;
            try{
                type=WxEventUrlType.valueOf(eventyKey);
            }catch(Exception e){
                logger.info("转化失败===="+e.getMessage());
            }
            content="已经关注了该公众号,已经关注事件-"+entity.getEvent().getName()+",进行跳转<a href='"+type.getUrl()+"'>"+type.getName()+"-"+type.getKey()+"</a>,(UserName="+entity.getToUserName()+",fromUser="+entity.getFromUserName()+"),ticket="+entity.getTicket();
        }else{
            content="初次关注公众号,事件"+entity.getEvent().getName()+",进行跳转<a href='http://www.weixin.com'>"+entity.getEventKey()+"</a>,(UserName="+entity.getToUserName()+",fromUser="+entity.getFromUserName()+")";
        }
        StringBuilder responseMsg=new StringBuilder("<xml>");
        //回复用户时，这里是用户的getToUserName；但用户发送过来消息这里是微信公众号的原始id
        responseMsg.append("<ToUserName><![CDATA[").append(entity.getToUserName()).append("]]></ToUserName>");
        responseMsg.append("<FromUserName><![CDATA[").append(entity.getFromUserName()).append("]]></FromUserName>");
        responseMsg.append("<CreateTime>").append(System.currentTimeMillis()).append("</CreateTime>");
        responseMsg.append("<MsgType><![CDATA[text]]></MsgType>");
        responseMsg.append("<Content><![CDATA[").append(content).append("]]></Content>");
        responseMsg.append("<MsgId>").append(entity.getMsgId()).append("</MsgId>");//消息id，随便填，但位数要够
        responseMsg.append("</xml>");
        return responseMsg.toString();
    }

    @Override
    public String generateScanEvent(WxTextModel entity) {
        StringBuilder responseMsg=new StringBuilder("<xml>");
        //回复用户时，这里是用户的getToUserName；但用户发送过来消息这里是微信公众号的原始id
        responseMsg.append("<ToUserName><![CDATA[").append(entity.getToUserName()).append("]]></ToUserName>");
        responseMsg.append("<FromUserName><![CDATA[").append(entity.getFromUserName()).append("]]></FromUserName>");
        responseMsg.append("<CreateTime>").append(System.currentTimeMillis()).append("</CreateTime>");
        responseMsg.append("<MsgType><![CDATA[text]]></MsgType>");
        responseMsg.append("<Content><![CDATA[").append("已经关注事件-"+entity.getEvent().getName()+",eventyKey="+entity.getEventKey()+",(UserName="+entity.getToUserName()+",fromUser="+entity.getFromUserName()+")").append("]]></Content>");
        responseMsg.append("<MsgId>").append(entity.getMsgId()).append("</MsgId>");//消息id，随便填，但位数要够
        responseMsg.append("</xml>");
        return responseMsg.toString();
    }

    @Override
    public String generateLocationEvent(WxTextModel entity) {
        StringBuilder responseMsg=new StringBuilder("<xml>");
        //回复用户时，这里是用户的getToUserName；但用户发送过来消息这里是微信公众号的原始id
        responseMsg.append("<ToUserName><![CDATA[").append(entity.getToUserName()).append("]]></ToUserName>");
        responseMsg.append("<FromUserName><![CDATA[").append(entity.getFromUserName()).append("]]></FromUserName>");
        responseMsg.append("<CreateTime>").append(System.currentTimeMillis()).append("</CreateTime>");
        responseMsg.append("<MsgType><![CDATA[text]]></MsgType>");
        responseMsg.append("<Content><![CDATA[").append("获取地理位置:（").append(entity.getLongitude()).append(",").append(entity.getLatitude()).append("）,Precision=").append(entity.getPrecision()).append("]]></Content>");
        responseMsg.append("<MsgId>").append(entity.getMsgId()).append("</MsgId>");//消息id，随便填，但位数要够
        responseMsg.append("</xml>");
        return responseMsg.toString();
    }

    @Override
    public String generateTextReply(WxTextModel entity) {
        String mediaId="RFKV7htAdRJukKne3zbDd0VC-ixvPXY7qLyo8ghpg49a0K6pEdboPPyG0rWV0Zgk";//TODO
        String content=entity.getContent();
        StringBuilder responseMsg=new StringBuilder("<xml>");
        //回复用户时，这里是用户的getToUserName；但用户发送过来消息这里是微信公众号的原始id
        responseMsg.append("<ToUserName><![CDATA[").append(entity.getToUserName()).append("]]></ToUserName>");
        responseMsg.append("<FromUserName><![CDATA[").append(entity.getFromUserName()).append("]]></FromUserName>");
        responseMsg.append("<CreateTime>").append(System.currentTimeMillis()).append("</CreateTime>");
        if(content.contains("图片")){
            responseMsg.append("<MsgType><![CDATA[image]]></MsgType>");
            responseMsg.append("<Image>");
            responseMsg.append("<MediaId><![CDATA["+mediaId+"]]></MediaId>");//TODO
            responseMsg.append("</Image>");
        }else if(content.contains("音乐")){
            responseMsg.append("<MsgType><![CDATA[music]]></MsgType>");
            responseMsg.append("<Music>");
            responseMsg.append("<Title><![CDATA[测试音乐]]></Title>");//TODO
            responseMsg.append("<Description><![CDATA[测试音乐反馈]]></Description>");
            responseMsg.append("<MusicUrl><![CDATA[http://94.191.88.231/mp3/2017/02/Five.mp3]]></MusicUrl>");
            responseMsg.append("<HQMusicUrl><![CDATA[http://94.191.88.231/mp3/2017/02/Five.mp3]]></HQMusicUrl>");
            responseMsg.append("<ThumbMediaId><![CDATA["+mediaId+"]]></ThumbMediaId>");
            responseMsg.append("</Music>");
        }else if(content.contains("图文")){
            responseMsg.append("<MsgType><![CDATA[news]]></MsgType>");
            responseMsg.append("<ArticleCount>2</ArticleCount>");
            responseMsg.append("<Articles>");
            responseMsg.append("<item>");
            responseMsg.append("<Title><![CDATA[").append("测试图文").append("]]></Title>");
            responseMsg.append("<Description><![CDATA[").append("图文简介").append("]]></Description>");
            responseMsg.append("<PicUrl><![CDATA[").append("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2746890508,1411167354&fm=26&gp=0.jpg").append("]]></PicUrl>");
            responseMsg.append("<Url><![CDATA[").append("https://www.baidu.com").append("]]></Url>");
            responseMsg.append("</item>");
            responseMsg.append("<item>");
            responseMsg.append("<Title><![CDATA[").append("测试图文2").append("]]></Title>");
            responseMsg.append("<Description><![CDATA[").append("图文简介2").append("]]></Description>");
            responseMsg.append("<PicUrl><![CDATA[").append("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2746890508,1411167354&fm=26&gp=0.jpg").append("]]></PicUrl>");
            responseMsg.append("<Url><![CDATA[").append("https://www.baidu.com").append("]]></Url>");
            responseMsg.append("</item>");
            responseMsg.append("</Articles>");
        }else if(content.contains("切换菜单")){
            //将用户的菜单进行切换
            List<String> openIdLists=new ArrayList<>();
            openIdLists.add(entity.getToUserName());//设置用户的openId
            if(content.contains("A")){
                responseMsg.append("<MsgType><![CDATA[text]]></MsgType>");
                responseMsg.append("<Content><![CDATA[").append("切换为A").append("]]></Content>");

                String accessToken= WeiXinUtils.getAccessToken(WeiXinUrlFinals.APPIDTEST,WeiXinUrlFinals.SECRETTEST);
                WeiXinUtils.unBatchtagging(accessToken,openIdLists,WeiXinUrlFinals.TAGB);
                WeiXinUtils.batchtagging(accessToken,openIdLists,WeiXinUrlFinals.TAGA);
            }else{
                responseMsg.append("<MsgType><![CDATA[text]]></MsgType>");
                responseMsg.append("<Content><![CDATA[").append("切换为B").append("]]></Content>");
                String accessToken= WeiXinUtils.getAccessToken(WeiXinUrlFinals.APPIDTEST,WeiXinUrlFinals.SECRETTEST);
                WeiXinUtils.unBatchtagging(accessToken,openIdLists,WeiXinUrlFinals.TAGA);
                WeiXinUtils.batchtagging(accessToken,openIdLists,WeiXinUrlFinals.TAGB);
            }
        }else{
            responseMsg.append("<MsgType><![CDATA[text]]></MsgType>");
            responseMsg.append("<Content><![CDATA[").append("[自动回复]").append(entity.getContent()).append("]]></Content>");
        }
        responseMsg.append("</xml>");
        return responseMsg.toString();
    }

    @Override
    public String generateDefault(WxTextModel entity) {
        StringBuilder responseMsg=new StringBuilder("<xml>");
        //回复用户时，这里是用户的getToUserName；但用户发送过来消息这里是微信公众号的原始id
        responseMsg.append("<ToUserName><![CDATA[").append(entity.getToUserName()).append("]]></ToUserName>");
        responseMsg.append("<FromUserName><![CDATA[").append(entity.getFromUserName()).append("]]></FromUserName>");
        responseMsg.append("<CreateTime>").append(System.currentTimeMillis()).append("</CreateTime>");
        responseMsg.append("<MsgType><![CDATA[text]]></MsgType>");
        responseMsg.append("<Content><![CDATA[").append("[自动回复]"+entity.getContent()).append("]]></Content>");
        responseMsg.append("<MsgId>").append(entity.getMsgId()).append("</MsgId>");//消息id，随便填，但位数要够
        responseMsg.append("</xml>");
        return responseMsg.toString();
    }

    public static void main(String[] args) {
        String eventyKey="abc";
        Optional<WxEventUrlType> typeOptional=Optional.ofNullable(WxEventUrlType.valueOf(eventyKey));
        WxEventUrlType type=typeOptional.get();
        System.out.println(type.getKey());
    }
}
